package net.sourceforge.jweb.jstl.fn;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

public class TimeUtil {
	private TimeUtil() {
	}

	public static Date now() {
		return new Date();
	}

	public static Date newDate(long mills) {
		return new Date(mills);
	}

	public static long getTime(Date d) {
		return d.getTime();
	}

	public static boolean before(Date d1, Date d2) {
		return d1.before(d2);
	}

	public static boolean after(Date d1, Date d2) {
		return d1.after(d2);
	}

	public static int compareTo(Date d1, Date d2) {
		return d1.compareTo(d2);
	}

	public static Calendar nowCalendar() {
		return Calendar.getInstance();
	}

	public static Calendar nowCalendar(TimeZone zone) {
		return Calendar.getInstance(zone);
	}

	public static Calendar nowCalendar(Locale locale) {
		return Calendar.getInstance(locale);
	}

	public static Calendar nowCalendar(TimeZone zone, Locale locale) {
		return Calendar.getInstance(zone, locale);
	}

	public static Locale[] getAvailableLocales() {
		return Calendar.getAvailableLocales();
	}

	public static Date getTime(Calendar c) {
		return c.getTime();
	}

	public static void setDate(Calendar c, Date d) {
		c.setTime(d);
	}

	public static long getTimeInMillis(Calendar c) {
		return c.getTimeInMillis();
	}

	public static void setTimeInMillis(Calendar c, long mills) {
		c.setTimeInMillis(mills);
	}

	public static int get(Calendar c, int filed) {
		return c.get(filed);
	}

	public static void set(Calendar c, int field, int value) {
		c.set(field, value);
	}

	public static void setMYD(Calendar c, int y, int m, int d) {
		c.set(y, m, d);
	}

	public static void setMYDH24M(Calendar c, int y, int m, int d, int h24, int mo) {
		c.set(y, m, d, h24, mo);
	}

	public static void setMYDH24MS(Calendar c, int y, int m, int d, int h24, int mo, int s) {
		c.set(y, m, d, h24, mo, s);
	}

	public static void clear(Calendar c) {
		c.clear();
	}

	public static void clear(Calendar c, int field) {
		c.clear(field);
	}

	public static boolean isSet(Calendar c, int f) {
		return c.isSet(f);
	}

	public static boolean before(Calendar c, Calendar c2) {
		return c.before(c2);
	}

	public static boolean after(Calendar c, Calendar c2) {
		return c.after(c2);
	}

	public static int compareTo(Calendar c, Calendar c2) {
		return c.compareTo(c2);
	}

	public static String getDisplayName(Calendar c, int field, int style, Locale locale) {
		return c.getDisplayName(field, style, locale);
	}

	public static Map<String, Integer> getDisplayNames(Calendar c, int field, int style, Locale locale) {
		return c.getDisplayNames(field, style, locale);
	}

	public static void add(Calendar c, int field, int amount) {
		c.add(field, amount);
	}

	public static void roll(Calendar c, int field, boolean up) {
		c.roll(field, up);
	}

	public static void roll(Calendar c, int field, int amount) {
		c.roll(field, amount);
	}

	public static void setTimeZone(Calendar c, TimeZone value) {
		c.setTimeZone(value);
	}

	public static TimeZone getTimeZone(Calendar c) {
		return c.getTimeZone();
	}

	public static int getFirstDayOfWeek(Calendar c) {
		return c.getFirstDayOfWeek();
	}

	public static int getMinimalDaysInFirstWeek(Calendar c) {
		return c.getMinimalDaysInFirstWeek();
	}

	public static int getMinimum(Calendar c, int field) {
		return c.getMinimum(field);
	}

	public static int getMaximum(Calendar c, int field) {
		return c.getMaximum(field);
	}

	private static final int ONE_DAY_MILLS = 1000 * 60 * 60 * 24;

	public static Integer daysBetween(Date d1, Date d2) {
		if (d1 == null || d2 == null)
			return null;
		return (int) ((d2.getTime() - d1.getTime()) / ONE_DAY_MILLS);
	}

	public static Integer daysToNow(Date d1) {
		return daysBetween(new Date(), d1);
	}
}
